﻿//************************************************************************//
//文件功能：网络函数集合
//创 建 人：薄帅
//创建时间：2011年05月26日
//修 改 人：
//修改时间：
//************************************************************************//

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;

namespace ProjBackEnd.Common.Helper
{
    public class Net
    {
        /// <summary>
        /// 获取本机IPv4格式的IP地址
        /// </summary>
        public static string GetLocalhostIPv4Address()
        {
            try
            {
                IPAddress[] addressList = Dns.GetHostAddresses(Dns.GetHostName());

                if (addressList.Length < 1)
                    return "";

                string strAddress = "";

                foreach (IPAddress ip in addressList)
                {
                    if (!ip.IsIPv6LinkLocal)
                    {
                        strAddress = ip.ToString();
                        break;
                    }
                }
                return strAddress;
            }
            catch
            {
                return "";
            }
        }

        /// <summary>
        /// 获取本机IPv6格式的IP地址
        /// </summary>
        public static string GetLocalhostIPv6Address()
        {
            try
            {
                IPAddress[] addressList = Dns.GetHostAddresses(Dns.GetHostName());

                if (addressList.Length < 1)
                    return "";

                string strAddress = "";

                foreach (IPAddress ip in addressList)
                {
                    if (ip.IsIPv6LinkLocal)
                    {
                        strAddress = ip.ToString();
                        break;
                    }
                }
                return strAddress;
            }
            catch
            {
                return "";
            }
        }
    }
}
